package dal

import (
	"context"

	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/errors"
	"lianzi_gos/pkg/util"

	"gorm.io/gorm"
)

// Get staff storage instance
func GetStaffDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.Staff))
}

// 员工表
type Staff struct {
	DB *gorm.DB
}

// Query staffs from the database based on the provided parameters and options.
func (a *Staff) Query(ctx context.Context, params schema.StaffQueryParam, opts ...schema.StaffQueryOptions) (*schema.StaffQueryResult, error) {
	var opt schema.StaffQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	db := GetStaffDB(ctx, a.DB)

	db = db.Where("status=?", "enabled")
	var list schema.Staffs
	pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	queryResult := &schema.StaffQueryResult{
		PageResult: pageResult,
		Data:       list,
	}
	return queryResult, nil
}

// Get the specified staff from the database.
func (a *Staff) Get(ctx context.Context, id string, opts ...schema.StaffQueryOptions) (*schema.Staff, error) {
	var opt schema.StaffQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	item := new(schema.Staff)
	ok, err := util.FindOne(ctx, GetStaffDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

// Exist checks if the specified staff exists in the database.
func (a *Staff) Exists(ctx context.Context, id string) (bool, error) {
	ok, err := util.Exists(ctx, GetStaffDB(ctx, a.DB).Where("id=?", id))
	return ok, errors.WithStack(err)
}

// Exist checks if the specified staff exists in the database.
func (a *Staff) ExistsStaff(ctx context.Context, sid string) (bool, error) {
	ok, err := util.Exists(ctx, GetStaffDB(ctx, a.DB).Where("uid=?", sid))
	return ok, errors.WithStack(err)
}

// Create a new staff.
func (a *Staff) Create(ctx context.Context, item *schema.Staff) error {
	result := GetStaffDB(ctx, a.DB).Create(item)
	return errors.WithStack(result.Error)
}

// Update the specified staff in the database.
func (a *Staff) Update(ctx context.Context, item *schema.Staff) error {
	result := GetStaffDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
	return errors.WithStack(result.Error)
}

// Delete the specified staff from the database.
func (a *Staff) Delete(ctx context.Context, id string) error {
	result := GetStaffDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Staff))
	return errors.WithStack(result.Error)
}

/**
通过 员工的手机号 判断是否有权限 有管理入口
*/
// Exist checks if the specified staff exists in the database.
func (a *Staff) ExistsPhone(ctx context.Context, phone string) (bool, error) {
	ok, err := util.Exists(ctx, GetStaffDB(ctx, a.DB).
		Where("status=?", "enabled").Where("phone=?", phone))
	return ok, errors.WithStack(err)
}

/**
通过 员工的手机号 判断是否 超级管理有权限 有管理入口
*/
// Exist checks if the specified staff exists in the database.
func (a *Staff) ExistsPhoneAdmin(ctx context.Context, phone string) (bool, error) {
	ok, err := util.Exists(ctx, GetStaffDB(ctx, a.DB).
		Where("status=?", "enabled").Where("phone=?", phone).Where("auth=?", 2))
	return ok, errors.WithStack(err)
}
